﻿using Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class UserInfoServies
    {
        //名字
        public static List<UserInfo> list;
        public static UserInfo p = new UserInfo();
        public static int User;
        public static List<UserInfo> SelectAlls(int UserID)
        {
            string sql = string.Format("select * from UserInfo where Uid=" + UserID);
            SqlDataReader dr = DBHelperSQL.GetDataReader(sql);
            List<UserInfo> list = new List<UserInfo>();
            while (dr.Read())
            {
                p.Uid = (int)dr["Uid"];
                
                p.NickName = dr["NickName"].ToString();
                p.Phone = dr["Phone"].ToString();
                p.Pwd = dr["Pwd"].ToString();
                p.Addc = dr["Addc"].ToString();
                p.UserType = dr["UserType"].ToString();
                p.Eml = dr["Eml"].ToString();

                list.Add(p);
            }
            dr.Close();
            return list;
        }
        public static List<UserInfo> SelectName(string Eml, string Pwd)
        {
            string sql = string.Format("select * from UserInfo where Eml='{0}' and  Pwd='{1}'", Eml, Pwd);
            SqlDataReader dr = DBHelperSQL.GetDataReader(sql);
            List<UserInfo> list = new List<UserInfo>();
            if (dr.Read())
            {
                UserInfo u = new UserInfo();
                u.Eml = dr["Eml"].ToString();
                u.Pwd = dr["Pwd"].ToString();
                list.Add(u);
                dr.Close();
                return list;
            }
            else
            {
                dr.Close();
                return null;

            }
           
        }


        //全查
        public static List<UserInfo> SelectAll()
        {
            string sql = "select * from UserInfo";
            SqlDataReader dr = DBHelperSQL.GetDataReader(sql);
            List<UserInfo> list = new List<UserInfo>();
            while (dr.Read())
            {
              //     private int Uid;
        //private int Classid;
      //  private string NickName;
      //  private string Phone;
      //  private string Pwd;
       // private string Addc;
       // private string UserType;
      //  private string Eml;
        UserInfo u = new UserInfo();
                u.Uid = (int)dr["Uid"];
                u.NickName = dr["UserName"].ToString();
                u.Phone = dr["Phone"].ToString();
                u.Pwd = dr["Pwd"].ToString();
                u.Addc = dr["Addc"].ToString();
                u.UserType = dr["UserType"].ToString();
                u.Eml = dr["Eml"].ToString();
                list.Add(u);
            }
            dr.Close();
            return list;
        }
        //模糊查
        public static UserInfo SelectByID(int id)
        {
            UserInfo u = new UserInfo();
            string sql = "select * from UserInfo where Uid=" + id;
            SqlDataReader dr = DBHelperSQL.GetDataReader(sql);
            List<UserInfo> list = new List<UserInfo>();
            while (dr.Read())
            {
                u.Uid = (int)dr["Uid"];
                u.NickName = dr["UserName"].ToString();
                u.Phone = dr["Phone"].ToString();
                u.Pwd = dr["Pwd"].ToString();
                u.Addc = dr["Addc"].ToString();
                u.UserType = dr["UserType"].ToString();
                u.Eml = dr["Eml"].ToString();
                list.Add(u);
            }
            dr.Close();
            return u;
        }
        //增
        public static bool Insert(UserInfo u)
        {
            string sql = string.Format("insert UserInfo values ('{0}','{1}',{2},'{3}','{4}','{5}')",
                u.NickName, u.Phone, u.Pwd, u.Addc, u.UserType,u.Eml);
            return DBHelperSQL.ExecuteNonQuery(sql);
        }
        //删
        public static bool Delete(int id)
        {
            string sql = "delete from UserInfo where Uid=" + id;
            return DBHelperSQL.ExecuteNonQuery(sql);
        }
        //改
        public static bool Update(int id, UserInfo u)
        {
            string sql = string.Format("update UserInfo set NickName='{0}' where Uid={1}",
                u.NickName, id);
            return DBHelperSQL.ExecuteNonQuery(sql);
            
        }
        public static bool UpdatePWD( string eml,string  pwd)
        {
            string sql = string.Format("update UserInfo set Pwd='{0}' where eml='{1}'",
               pwd, eml);
            return DBHelperSQL.ExecuteNonQuery(sql);
        }
        public static UserInfo SelectByUser(string qq)//根据qq查一条用户信息
        {
            string sql = string.Format("select * from UserInfo where Eml='{0}'", qq);
            SqlDataReader dr = DBHelperSQL.GetDataReader(sql);
            UserInfo u = new UserInfo();
            if (dr.Read())
            {
                u.Uid = dr.GetInt32(0);
                u.NickName = dr.GetString(1);
                u.Pwd= dr.GetString(2);
                u.Phone = dr.GetString(3);
                u.Eml = dr.GetString(4);
                u.Addc = dr.GetString(5);
                
            }
            dr.Close();
            return u;
        }
        public static bool Add(CommentF c)//增
        {
            string sql = string.Format("insert Comment values({0},{1},'{2}',5,GETDATE())",
                c.Uid, c.Foodid, c.Comments);
            return DBHelperSQL.ExecuteNonQuery(sql);
        }
        //public static bool Selectpp(string Eml, string Pwd)
        //{
        //    string sql = string.Format("SELECT UserType  FROM UserInfo where Eml='{0}' and  Pwd='{1}'", Eml, Pwd);

        //    return DBHelperSQL.ExecuteNonQuery(sql);

        //}
        public static bool Selectpp(string Eml, string Pwd)
        {
            string sql = string.Format("SELECT UserType  FROM UserInfo where Eml='{0}' and  Pwd='{1}'", Eml, Pwd);
            SqlDataReader dr = DBHelperSQL.GetDataReader(sql);
            list = new List<UserInfo>();
            if (dr.Read())
            {
                UserInfo u = new UserInfo();
                u.UserType = dr.GetString(0);
                if (u.UserType == "用户")
                {
                    dr.Close();
                    return true;

                }

                else if (u.UserType == "管理员")
                {
                    dr.Close();
                    return false;
                }

            }
            return false;

        }
        public static bool xg(string NickName, string Phone, string Pwd, string Addc, string UserType, string Eml, int Uid)
        {
            string sql = string.Format("update UserInfo set NickName='{0}',Phone='{1}',Pwd='{2}',Addc='{3}',UserType='{4}',Eml='{5}' where Uid={6}", NickName, Phone, Pwd, Addc, UserType, Eml, Uid);
            return DBHelperSQL.ExecuteNonQuery(sql);


        }
        public static void Lg(string Eml, string Pwd)
        {
            string sql = string.Format("select Uid from UserInfo where Eml='{0}' and Pwd='{1}'", Eml, Pwd);
            SqlDataReader dr = DBHelperSQL.GetDataReader(sql);
            while (dr.Read())
            {
                User = (int)dr["uid"];
            }
            dr.Close();
        }
        //根据id查找用户信息
        
       

    }
}
